package com.ddwl.goods.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.AcademyDao;
import com.ddwl.goods.dao.EssayCheckDao;
import com.ddwl.goods.dao.model.Academy;
import com.ddwl.goods.model.EssayCheck;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.AcademyCheckPageBo;
import com.ddwl.schema.bo.goods.EssayCheckBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.AcademyVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author hemiaojun
 * @Date 2020/11/20 10:49
 */
@Service
public class EssayCheckService {

    @Resource
    private EssayCheckDao essayCheckDao;
    @Resource
    private AcademyDao academyDao;


    @Transactional(rollbackFor = Exception.class)
    public void save(EssayCheckBo bo, String userId, String userName) {
        EssayCheck essayCheck = BeanConverUtils.convertBean(bo,EssayCheck.class);
        essayCheck.setCheckUserId(userId);
        essayCheckDao.insert(essayCheck);
        Academy academy = new Academy();
        academy.setId(bo.getEssayId());
        academy.setStatus(bo.getStatus());
        academy.setReason(bo.getReason());
        if (GoodsEnum.AcademyStatusEnum.PASSED.getCode().equals(bo.getStatus())) {
            academy.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        }else{
            academy.setEffect(BaseEnum.StatusEnum.DISABLE.getCode());
        }
        academyDao.updateByPrimaryKeySelective(academy);
    }

    public PageVo<AcademyVo> pageNeedCheckAcademy(PageBo<AcademyCheckPageBo> bo) {
        PageHelper.startPage(bo.getPage(), bo.getPageSize(), MySqlUtil.orderBy(bo.getSort(), bo.isAsc()));
        PageInfo<AcademyVo> info = new PageInfo<>(academyDao.getNeedCheckAcademyList(bo.getParam()));
        return new PageVo<>(info.getTotal(), bo.getPage(), bo.getPageSize(), info.getList());
    }
}
